package com.atung.parking.mapstruct;

import com.atung.parking.entity.BusMessagePublish;
import com.atung.parking.entity.BusParkingIotPackage;
import com.atung.parking.enums.MessageState;
import com.atung.parking.enums.SuperUserState;
import com.atung.parking.export.MessagePublishExportVO;
import com.atung.parking.response.page.MessagePublishResponse;
import com.atung.parking.response.page.ParkingIotPackageResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Mapper(componentModel = "spring")
public interface MessagePublishConverter {
    @Mappings({
            @Mapping(target = "creatorTime", source = "creatorTime", qualifiedByName = "timestampToString"),
            @Mapping(target = "messageState", source = "messageState", qualifiedByName = "setMessageState")
    })
    MessagePublishResponse toResponse(BusMessagePublish busMessagePublish);
    MessagePublishExportVO toExportVO(BusMessagePublish busMessagePublish);
    @Named("setMessageState")
    default String setMessageState(Integer status) {
        String description = MessageState.getDescriptionByCode(status);
        return description;
    }
    @Named("timestampToString")
    default String timestampToString(String timestamp) {
        if (timestamp == null || timestamp.isEmpty()) {
            return null;
        }
        try {
            long timeInMillis = Long.parseLong(timestamp);
            Date date = new Date(timeInMillis);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } catch (NumberFormatException e) {
            // 如果timestamp不是有效的长整型数字字符串，返回null或抛出异常
            return null;
        }
    }
}
